﻿#include "stockview.h"
#include "ui_stockview.h"

StockView::StockView(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::StockView)
{
    ui->setupUi(this);

    model=new QSqlQueryModel(this);
    ui->tableView->setModel(model);
}

StockView::~StockView()
{
    delete ui;
}

void StockView::setHeaderData()
{
    model->setHeaderData(0,Qt::Horizontal,QStringLiteral("日期"));
    model->setHeaderData(1,Qt::Horizontal,QStringLiteral("名称"));
    model->setHeaderData(2,Qt::Horizontal,QStringLiteral("数量"));
    model->setHeaderData(3,Qt::Horizontal,QStringLiteral("来源"));
    model->setHeaderData(4,Qt::Horizontal,QStringLiteral("备注"));
    model->setHeaderData(5,Qt::Horizontal,QStringLiteral("是否完成？"));
}

void StockView::on_btn_today_clicked()
{
    QSqlQuery query(QString("select date,name,number,provider,remark,done from td_stock where date = '%1'").arg(Env::date));
    model->setQuery(query);

    setHeaderData();
}

void StockView::on_btn_month_clicked()
{
    QString preDate=Env::month + "-01";
    QString postDate=Env::month + "-31";

    QString sql="select date,name,number,provider,remark,done from td_stock where date >= '%1' and date <= '%2' ";
    QSqlQuery query(sql.arg(preDate).arg(postDate));

    model->setQuery(query);

    setHeaderData();
}
